﻿using MedicalManagementAPP.AccountModule;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MedicalManagementAPP.Forms.DocForms
{
    public partial class InformationMaintenancetForm : Form
    {
        Person doc;
        public InformationMaintenancetForm(ref Person doc)
        {
            InitializeComponent();
            this.doc = doc;
            LoadInfo();
        }
        /// <summary>
        /// 根据用户ID来获取数据，显示在界面的各个控件上
        /// </summary>
        private void LoadInfo()
        {
            IDLabel.Text = "您的ID: " + doc.PId;
            foreach(string i in GetWorkerTypes())
            {
                typeComboBox.Items.Add(i);
            }
            foreach (string i in GetDepartments())
            {
                departmentComboBox.Items.Add(i);
            }
            DataRow workerData = AccountManager.GetWorkerInfo(doc.PId);
            // WorkerName,Password,DepartmentName,Phone,WorkerType
            nameTextBox.Text = workerData[0].ToString();
            pswTextBox.Text = workerData[1].ToString();
            departmentComboBox.Text = workerData[2].ToString();
            phoneTextBox.Text = workerData[3].ToString();
            typeComboBox.Text = workerData[4].ToString();
        }
        /// <summary>
        /// 得到所有的工种
        /// </summary>
        /// <returns>工种</returns>
        private string[] GetWorkerTypes()
        {
            string sql = "select WorkerType from Workers";
            DataTable data = Connection.SqlQuery(sql);
            string[] result = new string[data.Rows.Count];
            for(int i=0;i< data.Rows.Count; i++)
            {
                result[i] = data.Rows[i][0].ToString();
            }
            return result;
        }
        /// <summary>
        /// 得到所有的部门的名称
        /// </summary>
        /// <returns>部门的名称</returns>
        private string[] GetDepartments()
        {
            string sql = "select DepartmentName from Department";
            DataTable data = Connection.SqlQuery(sql);
            string[] result = new string[data.Rows.Count];
            for (int i = 0; i < data.Rows.Count; i++)
            {
                result[i] = data.Rows[i][0].ToString();
            }
            return result;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            string name = nameTextBox.Text;
            string psw = pswTextBox.Text;
            string depart = departmentComboBox.Text;
            string phone = phoneTextBox.Text;
            string workerType = typeComboBox.Text;

            doc.Update(name, workerType);
            string sql = "update Workers set "+
                "WorkerName=@name,Password=@psw,"+
                "DepartmentID=(select DepartmentID from Department where DepartmentName=@depart),"+
                "Phone=@phone,WorkerType=@workertype "+
                "where WorkerID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@name",name),
                new SqlParameter("@psw",psw),
                new SqlParameter("@depart",depart),
                new SqlParameter("@phone",phone),
                new SqlParameter("@workertype",workerType),
                new SqlParameter("@id",doc.PId)
            };
            if(!Connection.SqlNoQuery(sql, parameters)) { MessageBox.Show("修改失败，请检查数据正确性"); return; }
            MessageBox.Show("修改成功");

        }

        private void revokeButton_Click(object sender, EventArgs e)
        {
            LoadInfo();
        }
    }
}
